package hello.controller;

import hello.dto.Customer;
import hello.dto.Order;
import hello.repository.CustomerRepository;
import hello.repository.DatabaseClientRepository;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @author luo
 * @since 2022/9/15 14:37
 */
@RestController
@RequestMapping("/order")
@AllArgsConstructor
public class R2DBCController {

    private final CustomerRepository customerRepository;

    private final DatabaseClientRepository databaseClientRepository;


    @GetMapping("/byLastName")
    public Flux<Customer> byLastName(@RequestParam(defaultValue = "Bauer", required = false, value = "lasName") String lasName){
        return customerRepository.findByLastName(lasName);
    }

    @GetMapping("/list")
    public Flux<Customer> orderList(@RequestParam(value = "pageSize",defaultValue = "10",required = false)Integer pageSize) {
        return databaseClientRepository.pageCustomers(1,pageSize);
    }

    @GetMapping("/detail")
    public Flux<Customer> detail(@RequestParam(value = "id")Long id) {
        return databaseClientRepository.detail(id);//.blockLast();
    }

    @PostMapping("/byFirstLastName")
    public Flux<Customer> byFirstLastName(@RequestBody List<Customer> customers) {
        return databaseClientRepository.byFirstLastName(customers);
    }


    @PostMapping("/insertAndGetId")
    public Mono<Integer> insertAndGetId(@RequestBody Customer customer) {
        return databaseClientRepository.insertAndGetId(customer);
    }

}
